<?php

namespace app\admin\model;

use think\Model;

class PurchasePaymentOrder extends BaseModel
{
    protected $attributes = [
        'pay_type' => [
            '1' => 'TRC20:USDT',
            '2' => 'ERC20:USDT',
            '3' => 'ERC20:USDC',
            '4' => '人民币-支付宝',
            '5' => '人民币-微信',
            '6' => '人民币-对私',
            '7' => '人民币-对公',
            '8' => 'USD-香港账户',
            '9' => 'EUR-香港账户'
        ],
        'pay_type_message_group' => [
            '1' => [1, 21], // BOSS 财务人员
            '2' => [1], // BOSS
            '3' => [1], // BOSS
            '4' => [1, 21], // BOSS 财务人员
            '5' => [1, 21], // BOSS 财务人员
            '6' => [1], // BOSS
            '7' => [1, 20], // BOSS 财务主管
            '8' => [1], // BOSS
            '9' => [1] // BOSS
        ],
        'status' => [
             1 => '待审核',
             4 => '审核驳回',
             8 => '已撤销',
            20 => '待付款',
            30 => '已付款'
        ],
        'payment_account' => [
            '1' => '香港明曦EUR账号',
            '2' => '香港明曦USD账号',
            '3' => '人民币账号',
            '4' => 'USDT/USDC/BUSD'
        ]
    ];

    public function getFieldEnum($field)
    {
        if ('ALL' === strtoupper($field)) {
            return [
                'pay_type' => $this->attributes['pay_type'],
                'status' => $this->attributes['status'],
                'payment_account' => $this->attributes['payment_account']
            ];
        } else {
            return [$field => $this->$field];
        }
    }

    public function getPayTypeAttr($value, $data)
    {
        return intval($value);
    }

    public function getOwnerAttr($value, $data)
    {
        return intval($value);
    }

    public function getOwnerValue()
    {
        return $this->belongsTo(Operator::class, 'owner', 'id')->bind(['owner_value' => 'cname']);
    }

    public function getResponsibleAttr($value, $data)
    {
        return intval($value);
    }

    /**
     *  归属人
    */
    public function getResponsibleValue()
    {
        return $this->belongsTo(Operator::class, 'owner', 'id')->bind(['responsible_value' => 'cname']);
    }

    /**
     *  负责人
    */
    public function getResponsibleName()
    {
        return $this->belongsTo(Operator::class, 'responsible', 'id')->bind(['responsible_value' => 'cname']);
    }


    public function getStatusValueAttr($value, $data)
    {
        return isset($this->attributes['status'][$data['status']]) ? $this->attributes['status'][$data['status']] : '';
    }

    public function getPayTypeValueAttr($value, $data)
    {
        return isset($this->attributes['pay_type'][$data['pay_type']]) ? $this->attributes['pay_type'][$data['pay_type']] : '';
    }

    public function getPaymentAccountAttr($value, $data)
    {
        return intval($value);
    }

    public function getAttachment()
    {
        return $this->getFielUrl('attachment');
    }

    public function purchaseOrderNo()
    {
        return $this->belongsTo(PurchaseOrders::class, 'purchaseid', 'id')->bind(['purchase_no', 'total_amount','product_total_amount','other_amount','paid_amount' => 'pay_amount']);
    }

    public function getPaymentAccountValueAttr($value, $data)
    {
        return $this->attributes['payment_account'][$this->payment_account] ?? '';
    }

    public function getTotalAmountAttr($value,$data)
    {
        return number_format($data['product_total_amount'] + $data['other_amount'],2);
    }

    public function getPayTypeGroupEnum($pay_type)
    {
        $attributes = $this->attributes['pay_type_message_group'];
        return $attributes[(int)$pay_type];
    }

}
